import { Input, NavBar, TextArea } from 'antd-mobile'
import { useEffect, useState } from 'react'

import styles from './index.module.scss'

type Props = {
  onClose: () => void
  value: string
  type: '' | 'name' | 'intro'
  onUpdateProfile: (type: 'name' | 'intro', value: string) => void
}

const EditInput = ({ onClose, value, onUpdateProfile, type }: Props) => {
  const [name, setName] = useState(value)

  const isEditName = type === 'name'

  // 第一种解决无法正常显示简介的情况
  useEffect(() => {
    setName(value)
  }, [value])

  return (
    <div className={styles.root}>
      <NavBar
        className="navbar"
        right={
          <span
            className="commit-btn"
            onClick={() => {
              if (type === '') return
              onUpdateProfile(type, name)
            }}
          >
            提交
          </span>
        }
        onBack={onClose}
      >
        编辑{isEditName ? '昵称' : '简介'}
      </NavBar>

      <div className="edit-input-content">
        <h3>{isEditName ? '昵称' : '简介'}</h3>

        {isEditName ? (
          <div className="input-wrap">
            <Input
              value={name}
              placeholder="请输入"
              onChange={(value) => setName(value)}
            />
          </div>
        ) : (
          <TextArea
            rows={4}
            showCount
            value={name}
            maxLength={100}
            className="textarea"
            placeholder="请输入内容"
            onChange={(value) => setName(value)}
          />
        )}
      </div>
    </div>
  )
}

export default EditInput
